<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>垂体疾病用药</title>
    <link rel="stylesheet" href="../../../layui/css/layui.css" media="all">
    <script src="../../../js/jquery/1.x/jquery.js"></script>
    <script src="../../../layui/layui.js" charset="utf-8"></script>
    <script src="../../../js/config.js"></script>
    <script src="../../../js/function.js"></script>
    <script src="../../../js/public.js"></script>
    <link rel="stylesheet" href="../../../css/public.css">
    <link rel="stylesheet" href="../../../css/patient/qss.css">
    <style>
        .listOption{text-align:left;}
        .listOption span{cursor: pointer;color:#4777E7;}
    </style>
</head>
<body>
<div class="container">
    <div class="tab-title">
        <ul class="tab-title-tab">
            <li>
                <a href="../V1_1/patient_info.html">
                    <p>基本信息</p>
                    <p class="complete_basic">0%</p>
                </a>
            </li>
            <li class="tab-curr">
                <a>
                    <p>病历采集</p>
                    <p class="complete_qss">0%</p>
                </a>
            </li>
            <li>
                <a href="./qss7.html">
                    <p>检查信息</p>
                    <p style="display: none;">0%</p>
                </a>
            </li>
            <div class="clear"></div>
        </ul>
    </div>
    <div class="qss-container">
        <div class="qss-box">
            <div class="qss-box-title">
                <p>垂体疾病用药</p>
            </div>
            <form class="layui-form qss-form">
                <div class="qss-item">
                    <div class="qss-form-item layui-form-item switch-line">
                        <div class="qss-item-title layui-input-inline">
                            <p>1、是否使用垂体替代药物？</p>
                        </div>
                        <div class="layui-input-inline qss-input-swipch ">
                            <div class="switch3" id="switch-136" data-qid="136" title="是|否" option="0|1|2" value=""></div>
                        </div>
                    </div>
                </div>
                <div class="qss-item">
                    <div class="qss-item-title">
                        <p>2、为了控制疾病活动状态，垂体疾病药物治疗种类（备注:特指以垂体或肾上腺为靶点的药物治疗,非替代治疗）：</p>
                    </div>
                    <div class="qss-form-item layui-form-item">
                        <div class="qss-form-item-checkbox layui-form-item">
                            <input type="radio" title="一种" name="137" data-qid="137" class="cv-13701 js-zRadio" value="13701" lay-filter="radio1">
                            <input type="radio" title="两种" name="137" data-qid="137" class="cv-13702 js-zRadio" value="13702" lay-filter="radio1">
                            <input type="radio" title="三种" name="137" data-qid="137" class="cv-13703 js-zRadio" value="13703" lay-filter="radio1">
                            <input type="radio" title="三种以上" name="137" data-qid="137" class="cv-13704 js-zRadio" value="13704" lay-filter="radio1">
                            <input type="radio" title="无" name="137" data-qid="137" class="cv-13705 js-zRadio" value="13705" lay-filter="radio1">
                        </div>
                    </div>
                </div>
                <div class="qss-item">
                    <div class="qss-item-title">
                        <p>3、药物列表：</p>
                        <button type="button" id="btnAdd"><i class="layui-icon layui-icon-addition"></i>新增用药
                        </button>
                    </div>
                    <div class="qss-form-item layui-form-item">
                        <div class="qss-item-table">
                            <table class="layui-table" lay-even lay-skin="nob" style="border:1px solid #cccccc;">
                                <thead>
                                <tr>
                                    <th width="80">药物名称</th>
                                    <th width="80">每日总剂量</th>
                                    <th width="80">用法用量</th>
                                    <th width="80">开始日期</th>
                                    <th width="80">最新调整日期</th>
                                    <th width="80">结束日期</th>
                                    <th width="80">正在使用</th>
                                    <th width="80">备注</th>
                                    <th width="100">用药调整</th>
                                </tr>
                                </thead>
                                <tbody id="userDrugUsedList">

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>


                <div class="qss-item-btn">
                    <div class="qss-item-btn">
                        <a href="javascript:;">
                            <button type="button"  lay-submit="" lay-filter="submit-btn" data-type="1">提交并跳转下一页</button>
                        </a>
                        <a href="javascript:;">
                            <button type="button" lay-submit="" lay-filter="submit-btn" data-type="0">保存并留在本页</button>
                        </a>
                        <a href="qss4.html">
                            <button type="button">放弃编辑并跳转下一页</button>
                        </a>
                    </div>
                </div>
            </form>
            <div class="fixed-nav">
                <ul>
                    <li><a href="./qss1.html"><span>个人疾病史</span><span class="complete_qss1"></span><div class="clear"></div></a></li>
                    <li><a href="./qss2.html"><span>垂体疾病评估</span><span class="complete_qss2"></span><div class="clear"></div></a></li>
                    <li class="fixed-nav-curr"><a><span>垂体疾病用药</span><span class="complete_qss3"></span><div class="clear"></div></a></li>
                    <li><a href="./qss4.html"><span>合并症评估</span><span class="complete_qss4"></span><div class="clear"></div></a></li>
                    <li><a href="./qss5.html"><span>合并症用药</span><span class="complete_qss5"></span><div class="clear"></div></a></li>
                    <li><a href="./qss6.html"><span>垂体疾病治疗</span><span class="complete_qss6"></span><div class="clear"></div></a></li>
                </ul>
                <button type="button"  lay-submit="" lay-filter="submit-btn" data-type="1">提交</button>
            </div>
        </div>
    </div>

</div>
</body>
<script>
    var question = {
        136:[13602,'否','s1'],
        137:[0,'','s1'],
    }
    var patient_id = getLocalStorage('patient_id');
    var templateID = 15;
    var modeId = 1;

    function initForm(patient_id,callback) {
        ajax_get(configUrl.qssInfo,{user_id:patient_id,visit_level:1,template_id:13},(res)=>{
            $.each(res.data.question_data,function (index,item) {
                var qid = parseInt(item.question_id);
                if (qid == 136) {
                    if (item.answer.length > 0) {
                        if (item.answer[0].answer_id == (qid + '01')) {
                            $("input[name='q" + qid +"']").attr('checked',true);
                            $("#switch-"+qid).attr('value',1);
                        } else {
                            $("#switch-"+ qid).attr('value',2);
                            $("input[name='q" + qid +"']").attr('checked',false);
                        }
                        question[qid][0] = item.answer[0].answer_id;
                        question[qid][1] = item.answer[0].answer_value;
                    }
                }
                if (qid == 137) {
                    console.log(item);
                    if (item.answer.length > 0) {
                        $(".cv-" + item.answer[0].answer_id).attr('checked',true);
                        question[qid][0] = item.answer[0].answer_id;
                        question[qid][1] = item.answer[0].answer_value;
                    }
                }
            });
            callback();
        })
    }
    //获取用药列表
    function getUserDrugList(){
        ajax_get(configUrl.userDrugList,{user_id:patient_id,mode:1},function(res){
            console.log(res);
            if(res.status != 0){
                return;
            }
            $("#userDrugUsedList").empty();

            $.each(res.data.all,function(index,item){
                str = '<tr><td>'+ item.general_name +'</td><td>'+ item.drug_dose +  item.unit +'</td>';
                var useStr = '';
                if(parseInt(item.usage_dosage_morning) > 0) useStr += '早：' + item.usage_dosage_morning + item.unit;
                if(parseInt(item.usage_dosage_noon) > 0) useStr += '<br />中：' + item.usage_dosage_noon + item.unit;
                if(parseInt(item.usage_dosage_night) > 0) useStr += '<br />晚：' + item.usage_dosage_night + item.unit;
                if(parseInt(item.usage_dosage_bedtime) > 0) useStr += '<br/>睡前：' + item.usage_dosage_bedtime + item.unit;
                //其它用药
                if(item.usage_dosage_other.text){
                    useStr += '<br />'+ item.usage_dosage_other.text + '：'+ item.usage_dosage_other.mock + item.unit;
                }
                str += '<td>'+ useStr +'</td>'; //用法
                str += '<td>'+ item.start_date +'</td><td>'+ (item.adjust_date == '0001-01-01'?'' : item.adjust_date) +'</td>' +
                    '<td>'+ (item.end_date=='0001-01-01'?'':item.end_date) +'</td>';
                str += '<td>'+ (item.used_flag == 1  ? '是' : '否' )+'</td>';
                if(item.remark != ''){
                    remark = '<span title="'+ item.remark +'" style="cursor:pointer;">详情</span>'
                }else{
                    remark = '';
                }
                str += '<td>'+ remark +'</td>';
                str += '<td data-id="'+ item.id +'" class="listOption"><span data-type="view">查看用药</span>  ';
                if(item.used_flag == 1){
                    str += ' | <span data-type="change">调整剂量</span> <br /> <span data-type="stop">停止用药</span> ';
                }else{
                    str += '<br /> <span data-type="addAgain">重新用药</span>';
                }

                str += ' | <span data-type="delete">删除</span></td>'
                str += '</tr>';
                //console.log(str);
                $("#userDrugUsedList").append(str);
            });

        });
    }
    //删除用药
    function delDrug(id,callback){
        opID = getLocalStorage('doc_id');
        layer.confirm('确定要删除该药',{
            btn:['确定','取消'],
            title:'提示信息',
            icon:3
        },function(index) {
            ajax_post(configUrl.host + '/api/v1/pituitary/drug_del/' + id, {
                '_method': 'DELETE',
                'op_id': opID,
                'user_id': patient_id
            }, function (res) {
                callback(res);
                layer.close(index);
            })
        });
    }
    //停止用药
    function stopDrug(id){
        layer.open({
            type:2,
            title:'停止用药',
            closeBtn:1,
            shadeClose:true,
            area:['800px','550px'],
            content:'qss5_stop.html?mode_id='+ modeId+'&id='+ id,
        });

    }

    //重新用药
    function addAgain(id){
        layer.open({
            type:2,
            title:'重新用药',
            closeBtn:1,
            shadeClose:true,
            area:['800px','550px'],
            content:'qss5_again.html?mode_id='+ modeId+'&id='+ id,
        });
    }
    //剂量调整
    function changeDose(id){
        layer.open({
            type:2,
            title:'剂量调整',
            closeBtn:1,
            shadeClose:true,
            area:['800px','550px'],
            content:'qss5_edit.html?mode_id='+ modeId+'&id='+ id,
        });
    }
    //查看用草
    function viewDrug(id,cTitle){
        layer.open({
            type:2,
            title:'查看用药 '+ cTitle,
            closeBtn:1,
            shadeClose:true,
            area:['950px','300px'],
            content:'qss5_view.html?mode_id='+ modeId+'&id='+ id,
        });
    }
    $(document).ready(function () {
        var height = $(window).height() - 3;
        $('.container').css('min-height',height)
        $('.qss-container').css('min-height',height - 51);
        var patient_id = getLocalStorage('patient_id');
        getUserDrugList();

        //完整度
        getCompletion();
        //用药调整操作
        $(document).on('click','.listOption span',function(){
            var type = $(this).attr('data-type');
            var drugId = $(this).parent().attr('data-id');
            console.log('type='+ type + '; drugId='+ drugId);
            if(type == 'delete'){
                //删除单前行
                var that = $(this);
                delDrug(drugId,function(res){
                    if(res.status == 0){
                        that.parents().remove('tr');
                    }
                });
            }else if(type == 'stop'){
                stopDrug(drugId);
            }else if(type == 'change'){
                changeDose(drugId);
            }else if(type == 'view'){
                dTitle = $(this).parent().siblings().eq(0).html();
                dJiliang = $(this).parent().siblings().eq(2).html();
                console.log('dTitle',dTitle);
                dJiliang = dJiliang.replace(/<br>/g,' , ')
                viewDrug(drugId,dTitle + '【 ' + dJiliang + '】');
            }else if(type == 'addAgain'){
                addAgain(drugId);
            }
        });

        /*$(".js-zRadio").bind('mousedown',function(event){
            console.log('radio mouse doen');
            var radioChecked = $(this).prop('checked');
            $(this).prop('checked',!radioChecked);
            return false;
        });
        //阻止click事件默认行为
        $(".js-zRadio").click(function(){
            console.log('radio click')
            return false;
        });*/

        layui.config({
            base:'../../../layui/layui_exts/'
        }).extend({
                switch3: 'switch3/switch3'
            }
        ).use(['element', 'form', 'layer', 'laydate', 'laypage','switch3'], function () {
            var element = layui.element;
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var laypage = layui.laypage
            ,switch3 = layui.switch3;

            initForm(patient_id,function(){
                switch3.render({
                    elem:'.switch3',
                    done:function(val,item){
                        qid = item.parent().parent().attr('data-qid');
                        console.log('s-',val + '; qid='+ qid);
                        if(val == 0){
                            question[qid][0] = 0;
                            question[qid][1] = '';
                        }else if(val == 1){
                            question[qid][0] = qid + '01';
                            question[qid][1] = item.html();
                        }else if(val == 2){
                            question[qid][0] = qid + '02';
                            question[qid][1] = item.html();
                        }
                    }
                });
                form.render();
            });

            //新增用药点击
            $("#btnAdd").click(function(){
                layer.open({
                    type:2,
                    title:'新增用药',
                    closeBtn:1,
                    shadeClose:true,
                    area:['800px','550px'],
                    content:'qss5_add.html?mode_id='+ modeId,
                });
            });



            form.on('switch(switch1)',function (data) {
                var qid = $(this).attr('data-qid');
                if (this.checked) {
                    question[qid][0] = qid + '01';
                    question[qid][1] = '是';
                } else {
                    question[qid][0] = qid + '02';
                    question[qid][1] = '否';
                }
            });
            //测试点击取消
            form.on('radio(radio1)',function (data) {
                var qid = $(this).attr('data-qid');
                if (data.elem.checked) {
                    question[qid][0] = data.value;
                    question[qid][1] = $(this).attr('title');
                } else {
                    question[qid][0] = 0;
                    question[qid][1] = '';
                }

            })

            form.on('submit(submit-btn)',function () {
                var jumpType = $(this).attr('data-type');
                var chapterId = 1301;
                var visitLevel = 1;
                var params = {
                    'hosp_id':getLocalStorage('hosp_id'),
                    'dept_id':getLocalStorage('dept_id'),
                    'user_id':getLocalStorage('patient_id'),
                    'doc_id':getLocalStorage('doc_id'),
                    'visit_level':1,
                    'template_id':13,
                    'terminal_flg':0,
                    'data':[{
                        'visit_level':1,
                        'user_id':getLocalStorage('patient_id'),
                        'doc_id':getLocalStorage('doc_id'),
                        "terminal_flg":"0",
                        'template_id':13,
                        "datasource":"2",
                        'answer':[]
                    }
                    ]
                };
                var answer = [];
                $.each(question,function (qid,item) {
                    if (item[2] == 's1') {
                        if(question[qid][0] != 0) {
                            answer.unshift({
                                'chapter_id': chapterId,
                                'question_id': qid,
                                'subquestion_id': qid,
                                'answer_id': question[qid][0],
                                'answer_name': question[qid][1],
                                'visit_level': visitLevel
                            });
                        }
                    }
                });
                if(answer.length == 0){
                    layer.msg('没有选择项，请直接跳过');
                    return false;
                }
                params.data[0].answer = answer;
                ajax_post(configUrl.qssInfo,params,function(res){
                    if(res.status == '0'){
                        layer.msg('操作成功',{icon:6,time:1000},function(){
                            if(jumpType == 0){
                                //不跳转
                                window.location.reload();
                            }else{
                                //跳转
                                window.location.href = './qss4.html';
                            }
                        });
                    }else{
                        layer.msg(res.msg,{icon:5});
                    }
                });
                return false;
            })


        })
    })
</script>
</html>